package walletuserslogic

import (
	"context"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserWalletListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserWalletListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserWalletListLogic {
	return &UserWalletListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询用户钱包列表
func (l *UserWalletListLogic) UserWalletList(in *pb.UserWalletListReq) (*pb.UserWalletListResp, error) {
	defer utils.RecoverPanic()

	var err error
	var uws []*wallet.UserWallet
	uws, err = l.svcCtx.UserWalletModel.Find(l.ctx, in.Uid, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var rsp pb.UserWalletListResp
	rsp.List = make([]*pb.UserWalletInfo, 0)

	if len(uws) == 0 {
		return &rsp, nil
	}

	for i := 0; i < len(uws); i++ {
		var vl pb.UserWalletInfo
		vl.MoneyType = uws[i].MoneyType
		vl.MoneyAmount = decimal.NewFromInt(uws[i].Amount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		rsp.List = append(rsp.List, &vl)
	}
	return &rsp, nil
}
